package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.core.redis.RedisCache;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizStudyOrder;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyOrderFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyOrderQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyOrderVO;
import cn.sfrog.project.biz.admin.mapper.BizStudyOrderMapper;
import cn.sfrog.project.biz.admin.service.IBizStudyOrderService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 学研订单Service业务层处理
 */
@Service
public class BizStudyOrderServiceImpl extends ServiceImpl<BizStudyOrderMapper, BizStudyOrder> implements IBizStudyOrderService {
    @Resource
    private BizStudyOrderMapper bizStudyOrderMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询学研订单
     *
     * @param id 学研订单主键
     * @return 学研订单
     */
    @Override
    public BizStudyOrderVO selectBizStudyOrderById(Long id) {
        return bizStudyOrderMapper.selectBizStudyOrderById(id);
    }

    /**
     * 查询学研订单列表
     *
     * @param query 学研订单
     * @return 学研订单
     */
    @Override
    public List<BizStudyOrderVO> selectBizStudyOrderList(BizStudyOrderQueryVO query) {
        return bizStudyOrderMapper.selectBizStudyOrderList(query);
    }

    /**
     * 新增学研订单
     *
     * @param form 学研订单
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizStudyOrder(BizStudyOrderFormVO form) {
        BizStudyOrder bizStudyOrder = new BizStudyOrder();
        BeanUtils.copyProperties(form, bizStudyOrder);
        bizStudyOrder.setCreateTime(new Date());
        bizStudyOrder.setCreateBy(SecurityUtils.getUsername());
        bizStudyOrder.setUpdateTime(new Date());
        bizStudyOrder.setUpdateBy(SecurityUtils.getUsername());
        return bizStudyOrder.insert();
    }

    /**
     * 修改学研订单
     *
     * @param form 学研订单
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizStudyOrder(BizStudyOrderFormVO form) {
        BizStudyOrder bizStudyOrder = new BizStudyOrder();
        BeanUtils.copyProperties(form, bizStudyOrder);
        bizStudyOrder.setUpdateTime(new Date());
        bizStudyOrder.setUpdateBy(SecurityUtils.getUsername());
        return bizStudyOrder.updateById();
    }

    /**
     * 批量删除学研订单
     *
     * @param ids 需要删除的学研订单主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizStudyOrderByIds(Long[] ids) {
            return BizStudyOrder.DAO.delete(Wrappers.lambdaQuery(BizStudyOrder.class).in(BizStudyOrder::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
//        return BizStudyOrder.DAO.update(Wrappers.lambdaUpdate(BizStudyOrder.class).in(BizStudyOrder::getId, (Object[]) vo.getIds()).set(BizStudyOrder::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public BizStudyOrder createStudyOrder(Long userId, BigDecimal donateAmount, Long projectId,Long hospitalId) {
        BizStudyOrder studyOrder = new BizStudyOrder();
        studyOrder.setStudyOrderSn(redisCache.buildStudyOrderSerialNumber());
        studyOrder.setProjectId(projectId);
        studyOrder.setHospitalId(hospitalId);
        studyOrder.setNum(1);
        studyOrder.setAmount(donateAmount);
        studyOrder.setUserId(userId);
        studyOrder.insert();
        return studyOrder;
    }
}
